package cn.sumpu.app.guoguojie.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegExp {
	public static void main(String[] args) { 
        String str = "这是一个url链接http://www-test.company.com/view/1_2.html?a=%B8&f=%E4+%D3#tdsssss ok,thanks!<a>http://www.guoguojie.com/bonusDetail.htm?bonusId=5</a>"; 
        String regex = "(http:|https:)//[^[A-Za-z0-9\\._\\?%&+\\-=/#]]*"; 
//        String regex = "(http:|https:)//[\\w\\.\\-/:]+";
        Pattern pattern = Pattern.compile(regex); 
        Matcher matcher = pattern.matcher(str); 
        StringBuffer result = new StringBuffer(); 
        while (matcher.find()) { 
            StringBuffer replace = new StringBuffer(); 
            String url = matcher.group();
            if(url.startsWith("http://www.guoguojie.com")){
            	replace.append("<a href=\"").append(url); 
                replace.append("\" target=\"_blank\">"+url+"</a>"); 
            }else{
            	replace.append(url);
            }
            
            matcher.appendReplacement(result, replace.toString()); 
        } 
        matcher.appendTail(result); 
        System.out.println(str); 
        System.out.println(result); 
    }
}